<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f"%>
<%@ page import="java.util.List"%>
<%@ page isELIgnored="false" %> <%--isELIgnored=true禁用EL表达式--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>购物车</title>
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/main.css">
	<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.min-2.1.1.js"></script>
</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="fr">
				<div class="login_btn fl">
				    欢迎您：<em>${user.uName}</em>
                    <span>|</span
					<a href="loginPage">注销</a>
					<span>|</span>
					<a href="registerPage">注册</a>
				</div>
				<div class="user_link fl">
					<span>|</span>
					<a href="/user/centerPage">用户中心</a>
					<span>|</span>
					<a href="cartPage?uId=${user.id}">我的购物车</a>
					<span>|</span>
					<a href="/user/centerOrderPage?id=${id}">我的订单</a>
				</div>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="list?id=${id}" class="logo fl"><img src="<%=request.getContextPath() %>/images/logo.png" style="width: 150px;height: 100px;"></a>
		<div class="sub_page_name fl">|&nbsp;&nbsp;&nbsp;&nbsp;购物车</div>
		<div class="search_con fr">
			<input type="text" class="input_text fl" name="" placeholder="搜索商品">
			<input type="button" class="input_btn fr" name="" value="搜索">
		</div>		
	</div>

	<div class="total_count">全部商品<em>${count}</em>件</div>
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col02">商品单位</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
	<c:forEach items="${list}" var="obj">
	<ul class="cart_list_td clearfix">
	    <li class="col01"><input type="checkbox" name="k" checked value="${obj.gPrice * obj.buyCount}"></li>
	    <li class="col02"><img src="http://localhost:8887/${obj.imgUrl}"></li>
	    <li class="col03">${obj.gName}<br><em>${obj.gPrice}元/件</em></li>
	    <li class="col04">每件</li>
	    <li class="col05">${obj.gPrice}元</li>
	    <li class="col06">
            <div class="num_add">
                <a href="javascript:;" class="add fl">+</a>
                <input type="text" class="num_show fl" value="${obj.buyCount}"  id="sum">
                <a href="javascript:;" class="minus fl">-</a>
            </div>
        </li>
        <li class="col07">${obj.gPrice * obj.buyCount}元</li>
        <li class="col08"><a href="#" onclick="deleteGood(${obj.cartId})">删除</a><span>|</span><a href="#" onclick="submitOrder(${obj.gPrice}, ${obj.buyCount}, ${obj.gCount}, ${obj.id})">支付</a></li>
        <li class="col08"></li>
    </ul>
    </c:forEach>

	<div class="footer">
		<div class="foot_link">
			<a href="#">张旭光太胖</a>
			<span>|</span>
			<a href="#">张旭光太丑</a>
			<span>|</span>
			<a href="#">张旭光太虚</a>
			<span>|</span>
			<a href="#">健脑补肾丸</a>
		</div>
		<p> 北京旭光补肾技术有限公司 </p>
		<p>电话：541885418828    京ICP备*******8号</p>
	</div>
	
</body>
<script>
    function checkAll(obj){
        console.info($("k").val());
        var a = $(obj).prop("checked");
        if(a){
            $(":checkbox[name='k']").prop("checked", true);
        } else {
            $(":checkbox[name='k']").prop("checked", false);
        }
    }

    function deleteGood(id){
        console.info("id="+id);
        $.ajax({
            url:"deleteCart",
            data:"id="+id,
            type:"post",
            success: function (str){
                if(str == "1"){
                    alert("删除商品成功");
                    parent.location.reload();
                }else{
                    alert("删除商品失败");
                }
            }
        })
    }

    function submitOrder(gPrice, buyCount ,gCount, gId){
        var orderMoney = gPrice * buyCount;
        var remainder=gCount - buyCount;
        var params = "id="+gId+"&gCount="+remainder+"&orderMoney="+orderMoney+"&uId="+${user.id}+"&gPrice="+gPrice;
         $.ajax({
            url:"order",
            data:params,
            type:"post",
            success:function (str){
                if(str=="0"){
                    alert("生成订单失败");


                }else{
                    console.info(str);
                    alert("生成订单成功");

                    window.location.href="orderPage?id="+gId+"&buyCount="+buyCount+"&orderNo="+str;
                }
            }
        })
    }
</script>
</html>